Skip to content

2.7 官方脚手架

📝 模块更新日志 新特性*

+ 新增 `.NET9` 脚手架 4\.9\.1\.31 ⏱️2024\.02\.14 [3c7a25b](https://gitee.com/dotnetchina/Furion/commit/3c7a25b9903f26ccea88027acd7d485d5eab9da4)
+ 新增 `.NET8` 特有的 `Blazor Auto` 模式脚手架 4\.9\.1\.28 ⏱️2024\.02\.02 [67ea9ad](https://gitee.com/dotnetchina/Furion/commit/67ea9ad8df3c917a8b3eb8242e033d41cb512c20)
  • 突破性变化

    • 移除 .NET5 脚手架模板 4.9.5 ⏱️2024.08.09 edfc09b
    • 升级 脚手架支持创建 .NET8 Preview.1 项目 4.8.7 ⏱️2023.02.22
    • 问题修复

    • 修复 因调整 .NET5 脚手架模板导致 Swagger 无法访问问题 4.9.5.1 ⏱️2024.08.09 #IAJ128 edfc09b

    • 修复 Blazor 脚手架出现 blazor.server.js 不能加载问题(404) 4.8.7.13 ⏱️2023.03.16 #I6NOBQ
    • 其他更改

    • 调整 Blazor + WebAPI 脚手架模板,默认添加授权支持 4.8.7.37 ⏱️2023.04.07 #I6OM8O 544f80d

    • 调整 脚手架模板,默认启用主流文件类型 MIME 支持 4.8.7.5 ⏱️2023.03.07 e35cdab

特别说明脚手架不是安装在项目中的! 而是通过 CMDPowerShell 安装到操作系统中的。

2.7.1 脚手架

Furion 官方提供了多种 Web 应用类型的脚手架,方便大家快速创建多层架构项目。目前支持以下应用脚手架:

2.7.1.1 Furion + EFCore

模板类型 名称 版本 关键词 描述
nuget Furion.Template.Mvc nuget 👉 furionmvc Mvc 模板
nuget Furion.Template.Api nuget 👉 furionapi WebApi 模板
nuget Furion.Template.App nuget 👉 furionapp Mvc/WebApi 模板
nuget Furion.Template.Razor nuget 👉 furionrazor RazorPages 模板
nuget Furion.Template.RazorWithWebApi nuget 👉 furionrazorapi RazorPages/WebApi 模板
nuget Furion.Template.Blazor nuget 👉 furionblazor Blazor 模板
nuget Furion.Template.BlazorWithWebApi nuget 👉 furionblazorapi Blazor/WebApi 模板
nuget Furion.Template.Blazor.App nuget 👉 furionblazorapp Blazor App(Auto) 模板

2.7.1.2 Furion + SqlSugar

模板类型 名称 版本 关键词 描述
nuget Furion.SqlSugar.Template.Mvc nuget 👉 fsmvc Mvc 模板
nuget Furion.SqlSugar.Template.Api nuget 👉 fsapi WebApi 模板
nuget Furion.SqlSugar.Template.App nuget 👉 fsapp Mvc/WebApi 模板
nuget Furion.SqlSugar.Template.Razor nuget 👉 fsrazor RazorPages 模板
nuget Furion.SqlSugar.Template.RazorWithWebApi nuget 👉 fsrazorapi RazorPages/WebApi 模板
nuget Furion.SqlSugar.Template.Blazor nuget 👉 fsblazor Blazor 模板
nuget Furion.SqlSugar.Template.BlazorWithWebApi nuget 👉 fsblazorapi Blazor/WebApi 模板
nuget Furion.SqlSugar.Template.Blazor.App nuget 👉 fsblazorapp Blazor App(Auto) 模板

2.7.2 安装脚手架

打开 CMDPowershell 执行模板安装命令(旧版本 install 可换成 --install):

2.7.2.1 Furion + EFCore 脚手架安装

# Mvc 模板  
dotnet new install Furion.Template.Mvc  
# WebApi 模板  
dotnet new install Furion.Template.Api  
# Mvc/WebApi 模板  
dotnet new install Furion.Template.App  
# RazorPages 模板  
dotnet new install Furion.Template.Razor  
# RazorPages/WebApi 模板  
dotnet new install Furion.Template.RazorWithWebApi  
# Blazor 模板  
dotnet new install Furion.Template.Blazor  
# Blazor/WebApi 模板  
dotnet new install Furion.Template.BlazorWithWebAPI  
# Blazor App(Auto) 模板,仅支持 .NET8 和 .NET9  
dotnet new install Furion.Template.Blazor.App  

最新版安装不带版本号总是安装最新的版本。

2.7.2.2 Furion + SqlSugar 脚手架安装

# Mvc 模板  
dotnet new install Furion.SqlSugar.Template.Mvc  
# WebApi 模板  
dotnet new install Furion.SqlSugar.Template.Api  
# Mvc/WebApi 模板  
dotnet new install Furion.SqlSugar.Template.App  
# RazorPages 模板  
dotnet new install Furion.SqlSugar.Template.Razor  
# RazorPages/WebApi 模板  
dotnet new install Furion.SqlSugar.Template.RazorWithWebApi  
# Blazor 模板  
dotnet new install Furion.SqlSugar.Template.Blazor  
# Blazor/WebApi 模板  
dotnet new install Furion.SqlSugar.Template.BlazorWithWebAPI  
# Blazor App(Auto) 模板,仅支持 .NET8 和 .NET9  
dotnet new install Furion.SqlSugar.Template.Blazor.App  

最新版安装不带版本号总是安装最新的版本。

SqlSugar 脚手架基本使用在 SqlSugar 脚手架中,Core 层包含了一个 DbContext.cs 类,可通过 DbContext.Instance 就可以获取到 SqlSugarScope 对象。

数据库链接字符串在启动层 appsettings.json 文件的 ConnectionConfigs 节点中配置,对应的是 SqlSugar 中的 ConnectionConfig[] 对象。

2.7.3 使用脚手架

# 命令模板如下  
dotnet new 关键词 -n 项目名称 -f .NET版本  

# EFCore:支持创建 .NET5-8 版本  
dotnet new furionapi -n MyProject # 默认 .NET8  
dotnet new furionapi -n MyProject -f net5 # Furion 4.9.5+ 不再支持 .NET5  
dotnet new furionapi -n MyProject -f net6  
dotnet new furionapi -n MyProject -f net7  
dotnet new furionapi -n MyProject -f net8  
dotnet new furionapi -n MyProject -f net9  

# SqlSugar:支持创建 .NET5-8 版本  
dotnet new fsapi -n MyProject # 默认 .NET8  
dotnet new fsapi -n MyProject -f net5 # Furion 4.9.5+ 不再支持 .NET5  
dotnet new fsapi -n MyProject -f net6  
dotnet new fsapi -n MyProject -f net7  
dotnet new fsapi -n MyProject -f net8  
dotnet new fsapi -n MyProject -f net9  

这样就可以生成项目代码了,生成之后推荐将所有的 nuget 包更新到最新版本。

关于项目名称和 .NET 版本通过脚手架生成的项目名称不能包含 短横线 等特殊字符,如有该需求,可在生成之后通过 Visual Studio 进行手动修改。

-f 版本参数所有选项:net5net6net7net8net9,默认是 net8

特别提醒furionapi 对应的是上面列表的 关键词,我们也可以通过 dotnet new --list 查看。

想了解更多可以使用 dotnet new 关键词 --help 查看更多参数。

2.7.4 脚手架更新

只需要重新安装最新版替换即可,如:

dotnet new install Furion.Template.Api  

最新版安装不带版本号总是安装最新的版本。

2.7.5 脚手架卸载

  • 查看所有安装的脚手架模板
dotnet new uninstall  

  • 卸载指定脚手架模板
dotnet new uninstall Furion.Template.Api  

2.7.6 Visual Studio 集成

通过命令安装脚手架后,升级 Visual Studio 到最新版可使用可视化方式创建哦。

2.7.7 关于 MVC 添加区域出错问题

由于 Furion 底层依赖了 Microsoft.CodeAnalysis.CSharp 包,所以可能通过 Viusal Studio 的界面添加 Area 区域 时会出错,这时候只需要在 Web.Entry 层安装下面几个包即可:

  • Microsoft.CodeAnalysis.CSharp.Features
  • Microsoft.CodeAnalysis.CSharp.Scripting
  • Microsoft.CodeAnalysis.VisualBasic.Features
  • Microsoft.CodeAnalysis.Workspaces.MSBuild
  • Microsoft.VisualStudio.Web.CodeGeneration.Design

2.7.8 搭建脚手架

脚手架可以极大的提高我们搭建新项目的速度,推荐两篇文章给大家学习:

https://www.cnblogs.com/laozhang-is-phi/p/10205495.html

https://www.cnblogs.com/catcher1994/p/10061470.html

推荐直接拷贝 Furion 脚手架文件夹进行修改,可避免很多问题。

Furion 脚手架源码

2.7.9 反馈与建议

与我们交流给 Furion 提 Issue


了解更多想了解更多 模板知识 知识可查阅 dotnet-new 模板 章节。